/**
 * 类名:FunsionChartPanel
 * 别名:fcpanel
 * 说明:funsionChart Ext Panel 显示封装类
 * 属性说明:	swf: flash地址
 * 			dataurl: 数据源地址
 * 			params: 参数
 * 			datatype: 数据类型(xml,json,no:不自动加载数据),默认为xml
 * 			fctools: fcpanel的tools
 * 			maximization: 最大化(默认为true)
 * 			hyperlink: 超链接数组(数组的项为页面ID和页面说明,如[{id: 'PA03020703',name: '页面1'},{id: 'PA03020705' mame: '页面2'}])
 * 			explain: 页面的说明文字,
 * 			callback: function(){} 数据加载完成后的回调函数
 * 方法说明: 	setSwf()<设置flash地址,相对或绝对地址,(必须)>
 * 			setChartid()<不需要手动赋值,(不需要用户操作)>
 * 			setDataurl()<设置数据源地址,Action 或 Xml文件路径,(必须)>
 * 			setParams()<设置请求数据时携带参数,(可选)>
 * 			update(config)<更新图形操作,config所带属性:swf,dataurl,params;
 * 						   属性可选,缺失情况下为控件原有config属性>
 *  		updateByUrl(url)通过带参数的url直接更新数据
 *  		updateByXml(xml,swf)通过xml字符串直接更新
 * 			getChartXml()<获取控件的XML字符串.>
 * 			getChartAttribute(name)<获取chart的属性,name为属性名称.
 * 									如获取chart的标题:getChartAttribute('caption')
 * 									若属性不存在则放回空('')>
 * 			getChart()<获取控件的Chart对象.>
 * 			amplify()<放大图形方法>
 */

Ext.define('Sh.ux.FusionChartPanel', {  
	extend: 'Ext.panel.Panel',  
	alias: ['widget.fcpanel'],
	config : {                             
		swf: '',
 		chartid: '',
 		dataurl: '',
 		params: {},
 		datatype: '',
 		fctools: [],
 		maximization: true,
 		hyperlink: {},
 		explain: '',
 		callback: function(){}
    },
    initComponent: function(){
    	var me = this;
    	me.setChartid(me.id + '_chart');
    	me.on('afterrender',function(p){
    		me.update();
    	});
    	me.on('beforedestroy',function(p){
    		var chart = FusionCharts(p.id + '_chart');
			if(chart){
				chart.dispose();
			}
    	});
    	
    	me.on('boxready',function(p){
			me.preventDefaultMenu(me.body.dom);
		});
    	
    	me.callParent();
    },
    update: function(config){
    	var me = this;
    	if(config){
    		me.swf = config.swf || me.swf;
	    	me.dataurl = config.dataurl || me.dataurl;
	    	me.params = config.params || me.params;
    	}
    	var url = this.getDataurl();
    	
    	var chart = FusionCharts(me.getChartid());
    	
    	if(chart){
			chart.dispose();
		}
		var newchart = new FusionCharts(me.getSwf(), me.getChartid(), "100%", "100%", "0", "0");
		newchart.addEventListener('rendered', function () {
			if(Ext.getCmp(this.id.replace('_chart','')).callback)
			Ext.getCmp(this.id.replace('_chart','')).callback(me);
		});
		newchart.render(me.getEl().child('div.x-panel-body',true).id);
		me.body.mask("Loading");
		if('json' == me.datatype){
			newchart.setJSONData(me.getJsonData(url));
			me.body.unmask();
		}
		else if('no' == me.datatype){
			me.body.unmask();
		}
		else{
			Ext.Ajax.request({
			    url: url,
			    params: me.params,
			    success: function(response){
			    	var msg = response.responseText;
					newchart.setXMLData(response.responseText);
					me.body.unmask();
			    },failure: function (response, options) {
			    	me.body.unmask();
					var msg = response.responseText;
					newchart.setXMLData(response.responseText);
			    }
			});
			
		}
    },
    getChartXml: function(){
    	var chart = FusionCharts(this.getChartid());
    	if(chart){
    		
    		return chart.getXMLData();
    	}
    	
    	return '';
    },
    getChart: function(){
    	return FusionCharts(this.getChartid());
    },
    amplify: function(){
    	var p = this;
		Ext.create('Ext.window.Window', {
			title : p.title,
			maximized : true,
			modal: true,
			listeners : {
				afterrender : function(w) {
					var maxchart = new FusionCharts(p.getSwf(), w.id + '_chart', "100%", "100%", "0", "0");
					maxchart.setDataXML(p.getChartXml());
					maxchart.render(w.getEl().child('div.x-window-body',true).id);
				},
				beforedestroy: function(w){
					var maxchart = FusionCharts(w.id + '_chart');
					if(maxchart){
						maxchart.dispose();
					}
				}
			}
		}).show();
    },
	preventDefaultMenu:function(dom){
		var me = this;
		if(dom.addEventListener) {
			dom.addEventListener('mousedown', function (event) {
				if(2 === event.button) {
					event.stopPropagation(); 
					event.preventDefault(); 
				}
			}, true);
		}else{
			dom.attachEvent('onmousedown', function(){
				if(2 === event.button) {
					dom.setCapture();
				}
			});
			dom.attachEvent('onmouseup', function () {
				dom.releaseCapture();
			});
			dom.oncontextmenu = function () {
				return false;
			};
		}
	}
}); 
